home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MacIconFactory.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  155 lines

  1. /*
  2.  * @(#)MacIconFactory.java    1.4 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21.  
  22. package com.sun.java.swing.plaf.mac;
  23.  
  24. import com.sun.java.swing.*;
  25. import com.sun.java.swing.plaf.UIResource;
  26. import com.sun.java.swing.plaf.ComponentUI;
  27.  
  28. import java.awt.Graphics;
  29. import java.awt.Color;
  30. import java.awt.Component;
  31. import java.awt.Polygon;
  32. import java.io.Serializable;
  33.  
  34. /**
  35.  * Factory object that can vend Icons appropriate for the Macintosh L & F.
  36.  * <p>
  37.  * Warning: serialized objects of this class will not be compatible with
  38.  * future swing releases.  The current serialization support is appropriate
  39.  * for short term storage or RMI between Swing1.0 applications.  It will
  40.  * not be possible to load serialized Swing1.0 objects with future releases
  41.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  42.  * baseline for the serialized form of Swing objects.
  43.  *
  44.  * @version 1.4 02/02/98
  45.  * @author unknown
  46.  */
  47. public class MacIconFactory implements Serializable
  48. {
  49.  
  50.     private interface MacControlInterface {
  51.         static final int ENABLED_STATE = 0;
  52.         static final int PRESSED_STATE = 1;
  53.         static final int DISABLED_STATE = 2;
  54.         static final int    NUM_STATES = 3;
  55.     
  56.         static final int OFF = 0;
  57.         static final int ON = 1;
  58.         static final int    NUM_VALUES = 2;
  59.  
  60.         Icon getIcon(int state, int value);
  61.     }
  62.     
  63.     private static abstract class MacControlIcon implements MacControlInterface, Icon, UIResource, Serializable {
  64.  
  65.         public void paintIcon(Component c, Graphics g, int x, int y)
  66.         {
  67.             boolean isActive = true;
  68.             ButtonModel model = ((AbstractButton) c).getModel();
  69.             ComponentUI ui = ((AbstractButton) c).getUI();
  70.             if(ui instanceof MacRadioButtonUI)
  71.             {
  72.                 isActive = ((MacRadioButtonUI)ui).isActive();
  73.             }
  74.  
  75.             int isOn = model.isSelected() ? 1 : 0;
  76.             Icon img;
  77.  
  78.             if (!model.isEnabled() || !isActive)
  79.                 img = getIcon(DISABLED_STATE, isOn);
  80.             else if (model.isPressed() && model.isArmed())
  81.                 img = getIcon(PRESSED_STATE, isOn);
  82.             else
  83.                 img = getIcon(ENABLED_STATE, isOn);
  84.  
  85.             img.paintIcon(c, g, x, y);
  86.         }
  87.  
  88.         public int getIconWidth() {
  89.             return getIcon(ENABLED_STATE, OFF).getIconWidth();    // Assume all icons are the same size
  90.         }
  91.  
  92.         public int getIconHeight() {
  93.             return getIcon(ENABLED_STATE, OFF).getIconHeight();    // Assume all icons are the same size
  94.         }
  95.     }
  96.  
  97.     private static class MacRadioButtonIcon extends MacControlIcon
  98.     {
  99.         // Constructor of subclasses MUST fill in the icon values
  100.         protected static Icon[][] imageIcons = new Icon[NUM_STATES][NUM_VALUES];
  101.  
  102.         MacRadioButtonIcon()
  103.         {
  104.             if (imageIcons[ENABLED_STATE][OFF] == null)
  105.             {
  106.                 imageIcons[ENABLED_STATE][OFF]    = UIManager.getIcon("RadioButton.enabledOff");
  107.                 imageIcons[ENABLED_STATE][ON]    = UIManager.getIcon("RadioButton.enabledOn");
  108.                 imageIcons[PRESSED_STATE][OFF]    = UIManager.getIcon("RadioButton.pressedOff");
  109.                 imageIcons[PRESSED_STATE][ON]    = UIManager.getIcon("RadioButton.pressedOn");
  110.                 imageIcons[DISABLED_STATE][OFF]    = UIManager.getIcon("RadioButton.disabledOff");
  111.                 imageIcons[DISABLED_STATE][ON]    = UIManager.getIcon("RadioButton.disabledOn");
  112.             }
  113.         }
  114.         
  115.         public Icon getIcon(int state, int value)
  116.         {
  117.             return imageIcons[state][value];
  118.         }
  119.     }
  120.  
  121.     private static class MacCheckboxIcon extends MacControlIcon
  122.     {
  123.         // Constructor of subclasses MUST fill in the icon values
  124.         protected static Icon[][] imageIcons = new Icon[NUM_STATES][NUM_VALUES];
  125.  
  126.         MacCheckboxIcon()
  127.         {
  128.             if (imageIcons[ENABLED_STATE][OFF] == null)
  129.             {
  130.                 imageIcons[ENABLED_STATE][OFF]    = UIManager.getIcon("CheckBox.enabledOff");
  131.                 imageIcons[ENABLED_STATE][ON]    = UIManager.getIcon("CheckBox.enabledOn");
  132.                 imageIcons[PRESSED_STATE][OFF]    = UIManager.getIcon("CheckBox.pressedOff");
  133.                 imageIcons[PRESSED_STATE][ON]    = UIManager.getIcon("CheckBox.pressedOn");
  134.                 imageIcons[DISABLED_STATE][OFF]    = UIManager.getIcon("CheckBox.disabledOff");
  135.                 imageIcons[DISABLED_STATE][ON]    = UIManager.getIcon("CheckBox.disabledOn");
  136.             }
  137.         }
  138.         
  139.         public Icon getIcon(int state, int value)
  140.         {
  141.             return imageIcons[state][value];
  142.         }
  143.     }
  144.  
  145.     public static Icon getCheckBoxIcon()
  146.     {
  147.         return new MacCheckboxIcon();
  148.     }
  149.     
  150.     public static Icon getRadioButtonIcon()
  151.     {
  152.         return new MacRadioButtonIcon();
  153.     }
  154. }
  155.